package
{
	import com.greensock.TweenMax;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Dictionary;
	
	import fr.chrysalis.ui.UI;
	import fr.chrysalis.ui.UIConstants;
	import fr.chrysalis.ui.UIType;
	import fr.chrysalis.ui.assets.Asset;
	import fr.chrysalis.ui.assets.SWFAsset;
	import fr.chrysalis.ui.components.core.Container;
	import fr.chrysalis.ui.components.core.ContainerScrollable;
	import fr.chrysalis.ui.components.core.Font;
	import fr.chrysalis.ui.components.core.Form;
	import fr.chrysalis.ui.components.core.Style;
	import fr.chrysalis.ui.components.display.Button;
	import fr.chrysalis.ui.components.display.Label;
	import fr.chrysalis.ui.components.display.Loading;
	import fr.chrysalis.ui.components.display.ScrollBar;
	import fr.chrysalis.ui.components.display.ScrollBarMode;
	import fr.chrysalis.ui.components.events.ButtonEvent;
	import fr.chrysalis.ui.components.events.ComponentEvent;
	import fr.chrysalis.ui.components.events.FormEvent;
	import fr.chrysalis.ui.components.input.InputText;
	import fr.chrysalis.ui.events.UIEvent;
	import fr.chrysalis.ui.styles.Align;
	import fr.chrysalis.utils.Debug;
	
	import styles.StyleManager;
	
	import views.Connexion;
	
	public class AS3AirComponents extends Sprite
	{
		
		public var loading:Loading;
		public var txtLoading:Label;
		public var main:Container;
		public var bg:Container;
		public var scrollable:ContainerScrollable;
		public var connexion:Connexion;
		
		public function AS3AirComponents():void
		{	
			
			this.stage.scaleMode = StageScaleMode.NO_SCALE;
			this.stage.align = StageAlign.TOP_LEFT;
			this.stage.displayState = StageDisplayState.NORMAL;
			this.stage.quality = StageQuality.HIGH;
			
			var ui:UI = UI.getInstance();
			//UI.debug = true;
			ui.stage = this.stage;
			ui.add('ui/loading/sample1.swf', 'loading');
			ui.load();
			ui.addEventListener(UIEvent.COMPLETE, initMain);
			
		}
		
		private function initMain(evt:UIEvent):void {
			
			UI.dumpLibrary();
			
			this.loading = new Loading( new SWFAsset(UI.get('loading')) );
			this.loading.x = this.stage.stageWidth/2;
			this.loading.y = this.stage.stageHeight/2;
			this.loading.selfAlign = Align.MIDDLE_CENTER;
			
			this.txtLoading = new Label( new Asset() );
			this.txtLoading.text = "Chargement";
			this.txtLoading.x = this.stage.stageWidth/2 - this.loading.width / 2;
			this.txtLoading.y = this.stage.stageHeight/2 - (this.loading.height + 10);
			this.txtLoading.selfAlign = Align.MIDDLE_CENTER;
			
			this.addChild(this.loading.get());
			this.addChild(this.txtLoading.get());
			
			var ui:UI = UI.getInstance();
			ui.removeEventListener(UIEvent.COMPLETE, initMain);
			ui.add('ui/font/Arial.swf', 'arial', UIType.FONT);
			ui.add('ui/font/Calibri.swf', 'calibri', UIType.FONT);
			ui.add('ui/button/sample.swf', 'button');
			ui.add('ui/label/label.swf', 'label');
			ui.add('ui/input/text/sample1.swf', 'inputText');
			ui.add('ui/container/sample1.swf', 'bg1');
			ui.add('ui/container/sample2.swf', 'bg2');
			ui.add('ui/scrollbar/sample.swf', 'scrollbar');
			ui.load();
			ui.addEventListener(UIEvent.PROGRESS, onDataProgress);
			ui.addEventListener(UIEvent.COMPLETE, onDataLoaded);
			
		}
		
		/**
		 * Gestion du Loading 
		 */
		private function onDataProgress(evt:UIEvent):void {
			this.loading.setPourcent(evt.getPourcent());
			this.txtLoading.text = int(evt.getPourcent()) + '%';
		}
		
		private function onDataLoaded(evt:UIEvent):void {
			
			this.loading.dealloc();
			this.txtLoading.dealloc();
			
			new StyleManager();
			
			UI.dumpLibrary();
			Style.dump();
			Font.enumerateEmbededFonts();
			
			this.main = new Container( new Asset() );
			this.main.x = this.main.y = 0;
			this.main.width = this.stage.stageWidth;
			this.main.height = this.stage.stageHeight;
			
			this.bg = new Container( new SWFAsset(UI.get('bg2')) );
			this.bg.x = this.main.width/2;
			this.bg.y = this.main.height/2;
			this.bg.selfAlign = Align.MIDDLE_CENTER;
			this.bg.play('loop', false, false, -1);
			
			this.connexion = new Connexion();
			this.connexion.x = 100;
			this.connexion.y = 150;
			
			var connexion2:Connexion = new Connexion();
			connexion2.x = 100;
			connexion2.y = 150;
			connexion2.width = 100;
			
			//this.scrollable.addContent(this.connexion);			
			this.bg.addChild(this.connexion);
			this.connexion.addChild(connexion2);
			this.main.addChild(this.bg);
			this.addChild(main.get());
			
			this.stage.addEventListener(Event.RESIZE, scaleUI);
			
		}
		
		private function scaleUI(evt:Event):void {
			this.main.width = this.stage.stageWidth;
			this.main.height = this.stage.stageHeight;
			
			this.bg.x = this.main.getSize().width/2;
			this.bg.y = this.main.getSize().height/2;
		}
		
		
	}
}